﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
namespace 门诊医生工作站
{
    public partial class frm_SignUp : Form
    {
        public frm_SignUp()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            SqlConnection sqlConnection = new SqlConnection();//声明并实例化SQL连接
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;//借助App.config连接数据库
            DataSet dataSet = new DataSet();
            string sql = $"SELECT * FROM dbo.tb_Office";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection);
            sqlDataAdapter.Fill(dataSet, "dbo.tb_Office");
            DataRow dataRow = dataSet.Tables["dbo.tb_Office"].NewRow();
            dataRow[0] = -1;
            dataRow[1] = "--请选择--";
            dataSet.Tables["dbo.tb_Office"].Rows.InsertAt(dataRow, 0);
            this.cmb_Office.DataSource = dataSet.Tables["dbo.tb_Office"];
            this.cmb_Office.ValueMember = "No";
            this.cmb_Office.DisplayMember = "Name";

        }

        private void btn_SignUp_Click(object sender, EventArgs e)
        {
            if (this.txb_No.Text.Trim()=="")
            {
                MessageBox.Show("用户账号不能为空！");
                this.txb_No.Focus();
                return;
            }
            if (this.txb_Password.Text.Trim()=="")
            {
                MessageBox.Show("密码不能为空！");
                this.txb_Password.Focus();
                return;
            }
            if (this.txb_Name.Text.Trim()=="")
            {
                MessageBox.Show("姓名不能为空！");
                this.txb_Name.Focus();
                return;
            }
            if (this.cmb_Office.Text.Trim() == "--请选择--")
            {
                MessageBox.Show("科室不能为空！");
                this.cmb_Office.Focus();
                return;
            }
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString= ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlCommand sqlCommand = sqlConnection.CreateCommand();     //调用sqlConnection的方法CreateCommand创建sqlCommand并建立连接       
            sqlCommand.CommandText = $"INSERT dbo.tb_User( No, Password, Name,OfficeNo)VALUES(@No,HASHBYTES('MD5',@Password),@Name,@OfficeNo );";
            sqlCommand.Parameters.AddWithValue("@No", this.txb_No.Text);
            sqlCommand.Parameters.AddWithValue("@Password", txb_Password.Text);
            sqlCommand.Parameters.AddWithValue("@Name", this.txb_Name.Text);
            sqlCommand.Parameters.AddWithValue("@OfficeNo", this.cmb_Office.SelectedValue);
            sqlCommand.Parameters["@Password"].SqlDbType = SqlDbType.VarChar;
            int rowAffected=0;
            try
            {
            sqlConnection.Open();
            rowAffected=sqlCommand.ExecuteNonQuery();
            }
            catch (SqlException sqlEx)   //专门处理数据库异常
            {
                if (sqlEx.Number==2627)
                {
                    MessageBox.Show("请勿重复注册！");
                    this.txb_No.Focus();
                    this.txb_No.SelectAll();
                    this.txb_Password.Clear();
                    this.txb_Name.Clear();
                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
            } 
            finally
            {
               sqlConnection.Close();
            }
            
            if (rowAffected==1)   //成功写入一行记录
            {
                MessageBox.Show("注册成功！");
                this.Close();

            }
            
        }
    }
}
